package com.canplay.screen.network

import com.google.gson.Gson
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import java.lang.reflect.Type
import java.util.*

/**
 * Json数组解析处理 兼容异常
 */
class ArraySecurityAdapter: JsonDeserializer<List<*>> {

    override fun deserialize(
        json: JsonElement?,
        typeOfT: Type?,
        context: JsonDeserializationContext?
    ): List<*> {
        if (json == null ||!json.isJsonArray) {
            return Collections.EMPTY_LIST
        }
        return try {
            val newGson = Gson()
            return newGson.fromJson(json, typeOfT)
        } catch (e: NumberFormatException) {
            Collections.EMPTY_LIST
        }
    }
}